package com.fastloan.jn.mgt.customer.service.impl;


import com.alibaba.fastjson.JSON;
import com.calcuation.enums.ModelType;
import com.calcuation.model.JNCreditBusinessModel;
import com.calcuation.model.Message;
import com.fastloan.jn.common.BasicServiceImpl;
import com.fastloan.jn.mgt.customer.model.JnCustomer;
import com.fastloan.jn.mgt.customer.service.JnCustomerService;
import com.kawins.result.ResultEnum;
import com.kawins.result.ResultTo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.util.HtmlUtils;

import java.math.BigDecimal;

/**
 * @author chenkai
 *         createTime 2017-10-2017/10/16 20:28
 */
@Service
public class JnCustomerServiceImpl extends BasicServiceImpl<JnCustomer> implements JnCustomerService {
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultTo modelResult(String customerId, String jnCreditBusinessModelStr, int modelType) {
        switch (modelType) {
            case 0:
                ModelType.setModelType(ModelType.CREDIT_CPY);
                break;
            case 1:
                ModelType.setModelType(ModelType.CREDIT_INDI);
                break;
            case 2:
                ModelType.setModelType(ModelType.WARR_CPY);
                break;
            case 3:
                ModelType.setModelType(ModelType.WARR_INDI);
                break;
            case 4:
                ModelType.setModelType(ModelType.COLLE_CPY);
                break;
            case 5:
                ModelType.setModelType(ModelType.COLLE_INDI);
                break;
            default:
                break;
        }
        JNCreditBusinessModel jnCreditBusinessModel = JSON.parseObject(HtmlUtils.htmlUnescape(jnCreditBusinessModelStr), JNCreditBusinessModel.class);
        jnCreditBusinessModel.validateInput();
        Message result;
        try {
            result = jnCreditBusinessModel.getResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultTo(ResultEnum.OPERATION_FAILED, e.getMessage());
        }
        JnCustomer jnCustomer = selectByPrimaryKey(customerId);
        jnCustomer.setModelInfo(jnCreditBusinessModelStr);
        jnCustomer.setModelResult(BigDecimal.valueOf(result.getMatchineResult()));
        return updateByPrimaryKeySelective(jnCustomer) != 0 ? new ResultTo() : new ResultTo(ResultEnum.OPERATION_FAILED);
    }
}

